import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * httpServletRequest练习
 */
public class S17_HttpServletRequestExercise extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //解决urlencoding 导致的乱码
        req.setCharacterEncoding("UTF-8");
        //获取接收到的数据
        String username = req.getParameter("username");
        String pwd1 = req.getParameter("pwd1");
        String pwd2 = req.getParameter("pwd2");

        //获取checkbox
        String[] sports = req.getParameterValues("sport");
        //对sports 处理
        String sportsStr = "";
        for (String sport : sports) {
            sportsStr += sport + " ";
        }
        //获取radio
        String gender = req.getParameter("gender");
        //获取select
        String city = req.getParameter("city");
        //获取textarea
        String info = req.getParameter("info");

        //返回给浏览器，回显
        //设置浏览器端是utf-8, 而且类型是 text/html
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print("username= " + username + "<br/>");
        writer.print("pwd1= " + pwd1 + "<br/>");
        writer.print("pwd2= " + pwd2 + "<br/>");
        writer.print("喜欢的运动= " + sportsStr + "<br/>");
        writer.print("gender= " + gender + "<br/>");
        writer.print("city= " + city + "<br/>");
        writer.print("info= " + info + "<br/>");
        writer.flush();
        writer.close();
    }
}
